<?php

// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;
use OT\DataDictionary;
use Think\Model;

/**
 * 前台首页控制器
 * 主要获取首页聚合数据
 */
class MenusController extends HomeController{
    var $uid;
    function _initialize(){
        parent::_initialize();
        $this->uid=get_uid();

    }
    //增加菜单页面
    public function index(){
        $User = M("usermenu");
        $arrmenu = $User->where()->select();
        $User = M("member");
        $arruser = $User->where(array('uid' => $this->uid))->select();
        // dump($User->getLastSql());
        $User = M("usermenu_title");
        $arrtitle = $User->where()->select();
        foreach ($arrmenu as $v) {
            $v;
            //  print_r($v);
            foreach ($arrtitle as $x) {
                if ($v["menuid"] == $x["menuid"]) {
                    $x;
                    //    print_r($x);
                }
            }
        }
        $this->assign("arruser", $arruser);
        $this->assign("arrmenu", $arrmenu);
        $this->assign("arrtitle", $arrtitle);
        $this->display();
    }
    //报告操作结果
    public function add(){
        $User = M("usermenu");
        $arrmenu = $User->where()->select();
        $User = M("member");
        $arruser = $User->where(array('uid' => $this->uid))->select();
        // dump($User->getLastSql());
        $User = M("usermenu_title");
        $arrtitle = $User->where()->select();
        foreach ($arrmenu as $v) {
            $v;
            //  print_r($v);
            foreach ($arrtitle as $x) {
                if ($v["menuid"] == $x["menuid"]) {
                    $x;
                    //    print_r($x);
                }
            }
        }
        $this->assign("arruser", $arruser);
        $this->assign("arrmenu", $arrmenu);
        $this->assign("arrtitle", $arrtitle);
        $menunameid=I("post.menunameid");
        $nametitle=I("post.nametitle");
        $url=I("post.url");
        $uid=$this->uid;
        echo $uid;
        if($uid==1){
            if($menunameid and $nametitle and $url){
                $User=M("usermenu_title");
                $data[nametitle]=$nametitle;
                $data[url]=$url;
                $data[menuid]=$menunameid;
                $User->add($data);
                //dump($User->getLastSql());
                $this->display("index");
            }
            else{
                echo "输入有误，请重新输入";
            }
        }
        else{
            $abc="You don't have permission to";
            $this->assign("abc",$abc);
            $this->display();
        }

    }
    //删除
    public function delete(){
        $User = M("usermenu");
        $arrmenu = $User->where()->select();
        $User = M("member");
        $arruser = $User->where(array('uid' => $this->uid))->select();
        // dump($User->getLastSql());
        $User = M("usermenu_title");
        $arrtitle = $User->where()->select();
        foreach ($arrmenu as $v) {
            $v;
            //  print_r($v);
            foreach ($arrtitle as $x) {
                if ($v["menuid"] == $x["menuid"]) {
                    $x;
                    //    print_r($x);
                }
            }
        }
        $this->assign("arruser", $arruser);
        $this->assign("arrmenu", $arrmenu);
        $this->assign("arrtitle", $arrtitle);
        $this->display();
    }
    public function deleteok(){
        $User = M("usermenu");
        $arrmenu = $User->where()->select();
        $User = M("member");
        $arruser = $User->where(array('uid' => $this->uid))->select();
        // dump($User->getLastSql());
        $User = M("usermenu_title");
        $arrtitle = $User->where()->select();
        foreach ($arrmenu as $v) {
            $v;
            //  print_r($v);
            foreach ($arrtitle as $x) {
                if ($v["menuid"] == $x["menuid"]) {
                    $x;
                    //    print_r($x);
                }
            }
        }
        $this->assign("arruser", $arruser);
        $this->assign("arrmenu", $arrmenu);
        $this->assign("arrtitle", $arrtitle);

        $menutitleid=I("post.menutitleid");
        $User=M("usermenu_title");
        $uid=$this->uid;
        if($uid==1){
        $User->where("menutitleid=".$menutitleid."")->delete();
          //  dump($User->getLastSql());
            $acb="deleted successfully";
            $this->assign("acb",$acb);
            $this->display("delete");
        }
        else{
            $abc="You don't have permission to";
            $this->assign("abc",$abc);

        }


    }
    public function admin(){
        $uid=$this->uid;
        if($uid==1) {
            if (empty($id)) {
            } else {
                $User = M("usermenu_title");
                $User->where("menutitleid=" . $id . "")->delete();   //删除所选数据
            }
            $idd=I("get.p",1);
            $a=($idd-1)*10;
            $b=10;
            $Model = new Model();
            $arr = $Model->query("SELECT a.menutitleid,a.menuid,a.nametitle,b.menuname FROM ot_usermenu_title AS a LEFT JOIN ot_usermenu as b ON a.menuid=b.menuid ORDER BY a.menuid LIMIT $a,$b");
            dump($Model->getLastSql());
            $this->assign("arr", $arr);
            $arrr=$Model->query("SELECT a.menutitleid,a.menuid,a.nametitle,b.menuname FROM ot_usermenu_title AS a LEFT JOIN ot_usermenu as b ON a.menuid=b.menuid ORDER BY a.menuid");
            $row=count($arrr);
            $this->assign("row",$row);
            $count      = $row;// 查询满足要求的总记录数
            $Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
            $show       = $Page->show();// 分页显示输出
            // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
            //$list = $Model->where()->order('menutitleid')->limit($Page->firstRow.','.$Page->listRows)->select();
            $list=$Model->query("SELECT a.menutitleid,a.menuid,a.nametitle,b.menuname FROM ot_usermenu_title AS a LEFT JOIN ot_usermenu as b ON a.menuid=b.menuid ORDER BY a.menuid  LIMIT $Page->firstRow,$Page->listRows");
            $this->assign('list',$list);// 赋值数据集
            $this->assign('page',$show);// 赋值分页输出
            $this->display();
        }
        else{
            echo "You don't have permission to";
        }
    }
    public function  edit(){
        $this->display();
    }
}